var roleHar = require('role.har');
var roleHario = require('role.hario');
var roleHarvester = require('role.harvester');
var roleHarvesterio = require('role.harvesterio');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleUpgraderio = require('role.upgraderio');
var roleBuilderio = require('role.builderio');
var roleRepairer = require('role.repairer');
var roleCarrier = require('role.carrier');
var roleCarrierio = require('role.carrierio');

module.exports.loop = function () {
    

    for(var name in Memory.creeps) {
        if(!Game.creeps[name]) {
            delete Memory.creeps[name];
            console.log('Clearing non-existing creep memory:', name);
        }
    }

    var builder = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
    console.log('builder: ' + builder.length);

    if(builder.length < 4) {
        var newName = 'builder' + Game.time;
        console.log('Spawning new builder: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,MOVE], newName,
            {memory: {role: 'builder'}});
    }
    
    var builderio = _.filter(Game.creeps, (creep) => creep.memory.role == 'builderio');
    console.log('builderio: ' + builderio.length);

    if(builderio.length < 1) {
        var newName = 'builderio' + Game.time;
        console.log('Spawning new builderio: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,MOVE], newName,
            {memory: {role: 'builderio'}});
    }

    var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
    console.log('Harvesters: ' + harvesters.length);

    if(harvesters.length < 0) {
        var newName = 'harvester' + Game.time;
        console.log('Spawning new harvester: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
            {memory: {role: 'harvester'}});
    }

    var har = _.filter(Game.creeps, (creep) => creep.memory.role == 'har');
    console.log('Har: ' + har.length);

    if(har.length < 1) {
        var newName = 'har' + Game.time;
        console.log('Spawning new har: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,MOVE], newName,
            {memory: {role: 'har'}});
    }

    var hario = _.filter(Game.creeps, (creep) => creep.memory.role == 'hario');
    console.log('Hario: ' + hario.length);

    if(hario.length < 1) {
        var newName = 'hario' + Game.time;
        console.log('Spawning new hario: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,MOVE], newName,
            {memory: {role: 'hario'}});
    }
    
    var harvestersio = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvesterio');
    console.log('Harvestersio: ' + harvestersio.length);

    if(harvestersio.length < 0 ) {
        var newName = 'harvesterio' + Game.time;
        console.log('Spawning new harvesterio: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
            {memory: {role: 'harvesterio'}});
    }
    
    var upgrader = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
    console.log('upgrader: ' + upgrader.length);

    if(upgrader.length < 8) {
        var newName = 'upgrader' + Game.time;
        console.log('Spawning new upgrader: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
            {memory: {role: 'upgrader'}});
    }
    
    var upgraderio = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgraderio');
    console.log('upgraderio: ' + upgraderio.length);

    if(upgraderio.length < 8) {
        var newName = 'upgraderio' + Game.time;
        console.log('Spawning new upgraderio: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,MOVE], newName,
            {memory: {role: 'upgraderio'}});
    }
    
    var repairer = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
    console.log('repairer: ' + repairer.length);

    if(repairer.length < 2) {
        var newName = 'repairer' + Game.time;
        console.log('Spawning new repairer: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,MOVE], newName,
            {memory: {role: 'repairer'}});
    }

    var carrier = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrier');
    console.log('carrier: ' + carrier.length);

    if(carrier.length < 2) {
        var newName = 'carrier' + Game.time;
        console.log('Spawning new carrier: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([CARRY,MOVE], newName,
            {memory: {role: 'carrier'}});
    }

    var carrierio = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierio');
    console.log('carrierio: ' + carrierio.length);

    if(carrierio.length < 2) {
        var newName = 'carrierio' + Game.time;
        console.log('Spawning new carrierio: ' + newName);
        Game.spawns['Spawn1'].spawnCreep([CARRY,MOVE], newName,
            {memory: {role: 'carrierio'}});
    }
    
    if(Game.spawns['Spawn1'].spawning) { 
        var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name];
        Game.spawns['Spawn1'].room.visual.text(
            '🛠️' + spawningCreep.memory.role,
            Game.spawns['Spawn1'].pos.x + 1, 
            Game.spawns['Spawn1'].pos.y, 
            {align: 'left', opacity: 0.8});
    }

    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        if(creep.memory.role == 'har') {
            roleHar.run(creep);
        }
        if(creep.memory.role == 'hario') {
            roleHario.run(creep);
        }
        if(creep.memory.role == 'harvester') {
            roleHarvester.run(creep);
        }
        if(creep.memory.role == 'harvesterio') {
            roleHarvesterio.run(creep);
        }
        if(creep.memory.role == 'upgrader') {
            roleUpgrader.run(creep);
        }
        if(creep.memory.role == 'builder') {
            roleBuilder.run(creep);
        }
        if(creep.memory.role == 'upgraderio') {
            roleUpgraderio.run(creep);
        }
        if(creep.memory.role == 'builderio') {
            roleBuilderio.run(creep);
        }
        if(creep.memory.role == 'repairer') {
            roleRepairer.run(creep);
        }
        if(creep.memory.role == 'carrier') {
            roleCarrier.run(creep);
        }
        if(creep.memory.role == 'carrierio') {
            roleCarrierio.run(creep);
        }
    }
    
}